/*
 SGSortingGame.m
 Date Created: April 16, 2010
 
 Author: Karol Krizka <kkrizka@gmail.com>
 
 Copyright (C) 2010 Green-Power
 
 URL: http://code.google.com/p/green-power
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 $Id$
 
 */

#import "SGSortingGame.h"

#import "SGSortingGameLevel.h"

#import "PMProfile.h"

@implementation SGSortingGame 

@dynamic tutorialModeOn;
@dynamic score;
@dynamic numItemsProcessed;
@dynamic numItemsCorrectlySorted;
@dynamic owner;
@dynamic items;
@dynamic levels;

- (float)accuracy {
  float gameAccuracy = ((float)self.numItemsCorrectlySorted/(float)self.numItemsProcessed)*100;
  return gameAccuracy;
}

- (BOOL)tutorialModeOn {
  [self willAccessValueForKey:@"tutorialModeOn"];
  NSNumber *tutorialModeOnObj=[self primitiveValueForKey:@"tutorialModeOn"];
  [self didAccessValueForKey:@"tutorialModeOn"];
  return tutorialModeOnObj.boolValue;
}

- (void)setTutorialModeOn:(BOOL)newTutorialModeOn {
  [self willChangeValueForKey:@"tutorialModeOn"];
  [self setPrimitiveValue:[NSNumber numberWithBool:newTutorialModeOn] forKey:@"tutorialModeOn"];
  [self didChangeValueForKey:@"tutorialModeOn"];
}

- (NSUInteger)score {
  [self willAccessValueForKey:@"score"];
  NSNumber *scoreObj=[self primitiveValueForKey:@"score"];
  [self didAccessValueForKey:@"score"];
  return scoreObj.unsignedIntValue;
}

- (void)setScore:(NSUInteger)newScore {
  [self willChangeValueForKey:@"score"];
  [self setPrimitiveValue:[NSNumber numberWithUnsignedInt:newScore] forKey:@"score"];
  [self didChangeValueForKey:@"score"];
}

- (NSUInteger)numItemsProcessed {
  [self willAccessValueForKey:@"numItemsProcessed"];
  NSNumber *numItemsProcessedObj=[self primitiveValueForKey:@"numItemsProcessed"];
  [self didAccessValueForKey:@"numItemsProcessed"];
  return numItemsProcessedObj.unsignedIntValue;
}

- (void)setNumItemsProcessed:(NSUInteger)newNumItemsProcessed {
  [self willChangeValueForKey:@"numItemsProcessed"];
  [self setPrimitiveValue:[NSNumber numberWithUnsignedInt:newNumItemsProcessed] forKey:@"numItemsProcessed"];
  [self didChangeValueForKey:@"numItemsProcessed"];
}

- (NSUInteger)numItemsCorrectlySorted {
  [self willAccessValueForKey:@"numItemsCorrectlySorted"];
  NSNumber *numItemsCorrectlySortedObj=[self primitiveValueForKey:@"numItemsCorrectlySorted"];
  [self didAccessValueForKey:@"numItemsCorrectlySorted"];
  return numItemsCorrectlySortedObj.unsignedIntValue;
}

- (void)setNumItemsCorrectlySorted:(NSUInteger)newNumItemsCorrectlySorted {
  [self willChangeValueForKey:@"numItemsCorrectlySorted"];
  [self setPrimitiveValue:[NSNumber numberWithUnsignedInt:newNumItemsCorrectlySorted] forKey:@"numItemsCorrectlySorted"];
  [self didChangeValueForKey:@"numItemsCorrectlySorted"];
}

- (SGSortingGameLevel *)nextLevel {
  SGSortingGameLevel *lastLevel=self.lastLevel;
  NSUInteger nextLevelNumber=1;
  if(lastLevel!=nil) {
    nextLevelNumber=lastLevel.level+1;
  }
  
  SGSortingGameLevel *newLevel=(SGSortingGameLevel *)[NSEntityDescription insertNewObjectForEntityForName:@"SortingGameLevel" 
                                                                                    inManagedObjectContext:[self managedObjectContext]];

  newLevel.level=nextLevelNumber;
  [self addLevelsObject:newLevel];
  return newLevel;
}

- (SGSortingGameLevel *)lastLevel {
  NSSet *allLevels=self.levels;
  SGSortingGameLevel *currentLastLevel=nil;
  for(SGSortingGameLevel *currentLevel in allLevels) {
    if(currentLastLevel==nil || currentLastLevel.level < currentLevel.level) {
      currentLastLevel=currentLevel;
    }
  }
  return currentLastLevel;
  
  NSFetchRequest *fetchRequest=[[NSFetchRequest alloc] init];
  
  NSEntityDescription *sortingGameLevelEntitiy = [NSEntityDescription entityForName:@"SortingGameLevel"
                                                             inManagedObjectContext:self.managedObjectContext];
  [fetchRequest setEntity:sortingGameLevelEntitiy];
  [fetchRequest setFetchLimit:1];

   NSSortDescriptor *sortDescriptor=[[NSSortDescriptor alloc] initWithKey:@"level" ascending:NO];
   NSArray *sortDescriptors=[[NSArray alloc] initWithObjects:sortDescriptor,nil];
   [fetchRequest setSortDescriptors:sortDescriptors];
   [sortDescriptors release];
   [sortDescriptor release];
   
  
  NSArray *sortingLevelList=[self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
  [fetchRequest release];
  
  if(sortingLevelList.count == 0) return nil; // No existing level
  else return [sortingLevelList objectAtIndex:0];
}

@end
